home *** CD-ROM | disk | FTP | other *** search
- /*****************************************************************************
-
- mac_print.c. All rights reserved.
-
- Printing Routines.
-
- original version by Kevin Hammond
-
- Updated by MS and AS
- 27 Apr 1992 MS
- -- re-designed print engine
- 10 May 1992 MS
- -- added support for multiple copies
-
- updated: 10 May 1992 MS
-
- *****************************************************************************/
-
- #include "mac.h" /* Common */
-
- #pragma segment Print
-
- /* margins -- MS
- */
-
- #define PrTopMargin 20
- #define PrLeftMargin 20
- #define PrBottomMargin 20
- #define PrRightMargin 20
- #define PrTabChar ((char)'\t')
-
- #define NUM_HEADER_LINES 3
- #define TABSIZE 8
-
- /* TABSIZE -- MS: yes, I know most mac apps use 4, but input.c & ALL other
- * systems use 8 ... we want our code to parse OK don't we...
- */
-
- static THPrint hPrint = NIL; /* Printer record */
- static int prTabWidth;
-
-
- int PrDoc( char **hText, long count, THPrint hPrint, int font, int size,
- int pno, int mpno, char *mtitle);
- int MyDrawText( char *p, int count);
-
- extern CursHandle watchcurs; /* Watch cursor */
-
-
- TPPrPort pPrPort = NIL; /* Printer port */
- TPrStatus prStatus; /* Printing status */
-
-
- /* Candidates for Unloading */
-
- extern void eval(), abandon(), compileExp(), readCommand(),
- printExp();
-
-
-
-
- /*
- Do the page setup. -- kh
- */
-
- PageSetup()
- {
- PrOpen();
-
- if (PrError() == noErr)
- {
- MakePrintRecord();
- (void) PrStlDialog(hPrint);
- PrClose();
- }
- else
- SysBeep(1);
- }
-
-
- /*
- Setup the print job. -- kh
- */
-
- void PrintJobSetup()
- {
- PrOpen();
-
- if (PrError() == noErr)
- {
- MakePrintRecord();
- (void) PrJobDialog(hPrint);
- PrClose();
- }
- else
- SysBeep(1);
- }
-
-
-
- /*
- Make a new print record and initialise to default settings. -- kh
- */
-
- MakePrintRecord()
- {
- if (hPrint == NIL)
- {
- hPrint = (THPrint)NewHandle(sizeof(TPrint));
- PrintDefault(hPrint);
- }
- }
-
-
-
- /*
- Open the printer and driver. -- kh -- MS
- */
-
- OpenThePrinter(cancelled)
- Boolean *cancelled;
- {
- PrOpen();
-
- if (PrError() == noErr) /* Only do if no error */
- {
- MakePrintRecord();
-
- SetCursor(&(qd.arrow)); /* -- MS */
-
- *cancelled = !PrJobDialog(hPrint);
-
- SetCursor(*watchcurs); /* -- MS */
-
-
- updatewindows(); /* -- AS */
-
- if (*cancelled)
- pPrPort = NIL;
- else
- {
- pPrPort = PrOpenDoc(hPrint, NIL, NIL);
-
- /* Unload memory-intensive parts of the application */
- UnloadSeg(eval);
- UnloadSeg(abandon);
- UnloadSeg(compileExp);
- UnloadSeg(readCommand);
- UnloadSeg(printExp);
- }
-
- }
- else
- {
- SysBeep(1);
- *cancelled = TRUE;
- }
- }
-
-
- /*
- Close the printer.-- kh -- MS
- */
-
- void CloseThePrinter()
- {
-
- if (pPrPort != NIL)
- {
- /* Clear the printing port */
- PrCloseDoc(pPrPort);
-
- pPrPort = NIL;
- }
-
- if (hPrint != NIL)
- {
- /* Spool the file */
- if ((*hPrint)->prJob.bJDocLoop == bSpoolLoop && PrError() == noErr)
- PrPicFile(hPrint, NIL, NIL, NIL, &prStatus);
- }
-
- /* need to release memory locked above.
- * and close
- * MS - 27 Apr 1992
- */
-
- HUnlock((Handle)hPrint);
-
- PrClose();
-
- }
-
- /*
- * draw a line of text -- MS
- */
-
- MyDrawText( char *p, int count)
- {
- register char *p1, *p2;
- int len;
- Point pt;
-
- p1 = p;
- p2 = p+count;
- while (p<=p2) {
- while ((p1<=p2) && (*p1 != PrTabChar)) p1++;
- if ((len=p1-p)>0) DrawText( p, 0, p1-p);
- if (*p1==PrTabChar) {
- GetPen(&pt);
- Move((prTabWidth-(pt.h-PrLeftMargin)%prTabWidth), 0);
- p1++;
- }
- p = p1;
- }
- }
-
- /*
- * Print a document -- MS
- */
-
- PrDoc( char **hText, long count, THPrint hPrint, int font, int size,
- int pno, int noLines, char *mtitle)
- {
- register int line = 0;
- register int lastLineOnPage = 0;
- int mpno;
- int length;
- Rect printRect;
- int linesPerPage;
- int lineBase;
- int lineHeight;
- int headerLine;
- register char *ptr, *p1;
- FontInfo info;
- char thbuff[80];
- char *hbuff = thbuff;
- int hlen;
- char pfilename[256];
-
- strcpy(pfilename,mtitle);
- (void) c2pstr(pfilename);
- (**hPrint).prJob.pFileName = (StringPtr) pfilename;
-
- SetPort( &(pPrPort->gPort));
- TextFont( font);
- TextSize( size);
- printRect = (**hPrint).prInfo.rPage;
- GetFontInfo( &info);
- lineHeight = info.leading+info.ascent+info.descent;
- linesPerPage =
- (printRect.bottom-printRect.top-PrTopMargin-PrBottomMargin)/lineHeight;
- linesPerPage -= NUM_HEADER_LINES; /* assuming linesPerPage>HEADER */
- mpno = (noLines / linesPerPage);
- if (noLines%linesPerPage != 0)
- mpno++;
- HLock( hText);
- ptr = p1 = (*hText);
- do
- {
- if (PrError() != noErr) /* cancel or error trap */
- break;
-
- PrOpenPage( pPrPort, NIL);
- updatewindows();
- if (PrError() == noErr) /* cancel or error trap */
- {
- lastLineOnPage += linesPerPage;
- MoveTo( printRect.left+PrLeftMargin,
- (lineBase = printRect.top+lineHeight) );
- /* the header */
- for( headerLine=1 ; headerLine <= NUM_HEADER_LINES ; ++headerLine)
- {
- switch (headerLine)
- {
- case 1:
- hlen = sprintf( hbuff,
- "Page %i of %i : %s\n",
- pno++, mpno, mtitle);
- MyDrawText( hbuff, hlen);
- break;
- case 2:
- Line( printRect.right -
- printRect.left -
- PrLeftMargin -
- PrRightMargin, 0);
- break;
- default:
- break;
- }
- MoveTo( printRect.left+PrLeftMargin, (lineBase += lineHeight));
- }
- do
- {
- /* PrintLine: */
- while ((ptr<(*hText)+count) &&
- ( (*(ptr+1) != (char)'\r') &&
- (*(ptr+1) != (char)'\n') ) ) ptr++;
- if (ptr<(*hText)+count)
- ptr++;
- if ((length=(int)(ptr-p1)-1)>0)
- MyDrawText( p1, length);
- MoveTo( printRect.left+PrLeftMargin, (lineBase += lineHeight));
- p1 = ptr;
- }
- while ((++line != lastLineOnPage) && (ptr<(*hText)+count));
- }
- PrClosePage( pPrPort);
- }
- while (ptr<(*hText)+count);
- HUnlock(hText);
- }
-
- /*
- Print the front window. -- modified by MS
- */
-
- Print_Window(windex)
- int windex;
- {
- char *fwinname;
- char wname[256];
- GrafPtr SavedPort;
- Boolean cancelled;
- FontInfo info;
-
- if(isLegalWindow(windex))
- {
- getwtitle( WINDOW(windex), wname);
- fwinname = wname;
-
- GetFontInfo( &info );
-
- GetPort(&SavedPort); /* Get the current window port */
-
- CheckMemory("Print",16000 /* guess */);
- OpenThePrinter(&cancelled);
-
- if (pPrPort != NIL && hPrint != NIL && !cancelled)
- {
- TEHandle teh;
- Handle hteh;
- int thisCopy; /* Which copy are we printing.. */
- int maxCopies; /* How many to print */
- int stpno; /* starting page number */
-
- teh = TEHANDLE(windex);
-
- SetCursor(*watchcurs);
-
- TEHLock(windex);
- hteh = (Handle) ((*teh)->hText);
-
- stpno = 1;
-
- /* Print it
- */
-
- maxCopies = (int) ( (*hPrint)->prJob.iCopies );
-
- for (thisCopy = 1; thisCopy <= maxCopies && PrError() == noErr;
- thisCopy++)
- {
- prTabWidth = TABSIZE * info.widMax;
- PrDoc( hteh, (*teh)->teLength, hPrint,
- (*teh)->txFont, (*teh)->txSize,
- stpno, (*teh)->nLines, fwinname);
- }
-
- TEHUnlock(windex);
- }
-
- else if (!cancelled)
- SysBeep(1);
-
- CloseThePrinter(); /* Close the printer */
- SetPort(SavedPort);
- InitCursor();
- }
- }
-